//
//  LXScanerController.m
//  绿稀全球购
//
//  Created by 王敏 on 16/6/14.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LXScanerController.h"

#import "LXDScanView.h"

#import <AVFoundation/AVFoundation.h>

@interface LXScanerController ()<LXDScanViewDelegate,UIAlertViewDelegate>
@property (nonatomic, strong) LXDScanView * scanView;
@end

@implementation LXScanerController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpNavi];
    [self setUpScanView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNoti:) name:@"applicationDidBecomeActive" object:nil];
}


-(void)handleNoti:(NSNotification*)noti{
    if ([noti.name isEqualToString:@"applicationDidBecomeActive"]) {
        if(self.scanView){
            [self.scanView start];
            [self.scanView timerStart];
        }
    }
}
-(void)setUpNavi{
    self.navigationItem.title = @"扫描二维码";
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
}

-(void)setUpScanView{
    
#if TARGET_IPHONE_SIMULATOR//模拟器
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"" message:@"抱歉模拟器不支持扫一扫功能" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    alert.delegate = self;
    [alert show];
#elif TARGET_OS_IPHONE//真机
    
    
    NSString *mediaType = AVMediaTypeVideo;//读取媒体类型
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态
    if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
        [self.view alertWithTitle:@"" mesege:@"相机权限未开启，无法使用扫一扫功能。请前往\"设置\"->\"绿稀全球购\"开启相机权限\n或者:" buttons:@[@"现在设置",@"取消"] handle:^(NSInteger buttonIndex) {
            if (buttonIndex == 0) {
                
            }else{
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
    }
    else
    {
        LXDScanView * scanView = [LXDScanView scanViewShowInController:self];
        [self.view addSubview:scanView];
        self.scanView = scanView;
    }

#endif

}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self.scanView start];
    [self.scanView timerStart];
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleDefault];

//    [self.scanView stop];
    [self.scanView timerStop];
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark - LXDScanCodeController

/**
 *  返回扫描结果
 */
- (void)scanView:(LXDScanView *)scanView codeInfo:(NSString *)codeInfo
{
    
    if([codeInfo rangeOfString:@"http://weixin.qq.com/"].location !=NSNotFound){
       codeInfo =  [codeInfo stringByReplacingOccurrencesOfString:@"http://weixin.qq.com/" withString:@"weixin://"];
    }
    NSURL * url = [NSURL URLWithString: codeInfo];
    if (![[UIApplication sharedApplication] canOpenURL: url]) {
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @"警告" message: [NSString stringWithFormat: @"%@:%@", @"无法解析的二维码", codeInfo] delegate: nil cancelButtonTitle: @"确定" otherButtonTitles: nil];
        [alertView show];
    } else {
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"扫描结果" message:codeInfo preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * openUrl = [UIAlertAction actionWithTitle:@"打开链接" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[UIApplication sharedApplication] openURL:url];
        }];
        
        UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self.navigationController popViewControllerAnimated:YES];
        }];
        [alert addAction:openUrl];
        [alert addAction:cancel];
        [self presentViewController:alert animated:YES completion:nil];
    }
    
    
    
}


-(void)dealloc{
    [self.scanView stop];
    [self.scanView removeFromSuperview];
    self.scanView = nil;
//    NSLog(@"扫描控制器 %s",__func__);
}
@end
